home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / mike40c.arc / MAKARRAY.C < prev    next >
Text File  |  1986-10-24  |  3KB  |  121 lines

  1. /**********************************************************************
  2.   makarray.c       compile:  msc makarray.c;    for Microsoft C 4.0
  3.                              link makarray;
  4.  
  5.   Makarray will convert any file into C array format.  It was written
  6.   to allow 4000 byte screen dumps, like the .bin files generated by
  7.   ANSIPAINT,  to be linked into a C program and displayed by the
  8.   putscrn(screen array, video page)  function.  The output for
  9.   makarray() is sent to stdout and can be redirected to any .c or .h
  10.   file.  The resulting char * is called hexdata, but can be changed
  11.   to any name.
  12.  
  13.      Usage:   makarray filename >screen.h
  14.  
  15.      10/24/86    by Mike Elkins   Mike's C Board -- 619-722-8724
  16.  
  17. ************************************************************************/
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <fcntl.h>
  21. #define BUFSIZE 512
  22.  
  23. extern int errno;
  24. char buffer[BUFSIZE];    /* input buffer */
  25.  
  26. main(argc,argv)         /* Generate arrar */
  27. int  argc;
  28. char *argv[]; 
  29. {
  30.     char c;
  31.     unsigned int i,numin,tot,file,cfrom;
  32.  
  33.     if (argc < 2) {
  34.         abort("Usage:makarray filespec ",0,0);
  35.     }
  36.  
  37.     if ((file=open(argv[1],O_BINARY)) == -1) {
  38.         abort("cannot open",argv[1],errno);
  39.     }
  40.  
  41.     tot = 0;
  42.  
  43.     /*    read and dump BUFSIZE at a time      */
  44.  
  45.     printf("char  hexdata[] = { \n");
  46.     do {
  47.         numin=read(file,buffer,BUFSIZE);
  48.         tot += numin;
  49.         if (numin == -1) {
  50.             abort("cannot read",argv[1],errno);
  51.         }
  52.         cfrom=0;
  53.         while (cfrom < numin) {
  54.  
  55.  
  56.             /*    print 16 bytes in hex    */
  57.  
  58.             for (i=0; i < 16; i++) {
  59.                 putchar('0');
  60.                 putchar('x');
  61.                 ohb(buffer[cfrom++]);
  62.                 putchar(',');
  63.             }
  64.  
  65.             /*    print the bytes in ascii as a comment    */
  66.             /****  REMOVE COMMENTS TO ENABLE ASCII  *****/
  67.  
  68.             /*         printf(" /* ");                 */
  69.             /*            for (i=0; i < 16; i++) {     */
  70.             /*                c = buffer[cfrom] & 0x7f;*/
  71.             /*                if ( isprint(c) )        */
  72.             /*                    putchar(c);          */
  73.             /*                else                     */
  74.             /*                    putchar('.');        */
  75.             /*                cfrom++;                 */
  76.             /*                }                        */
  77.             /*         printf(" * /");    */   /* remove space in comment */
  78.  
  79.             putchar('\n');
  80.         }
  81.     }
  82.     while (numin == BUFSIZE);
  83.  
  84.     printf("0x00   };    /* %u bytes */", tot);     /* terminate w/NULL */
  85. }
  86.  
  87. ohb(byt)            /*    print a byte in hex    */
  88. char byt; 
  89. {
  90.     onib(byt>>4);
  91.     onib(byt);
  92. }
  93.  
  94. onib(nib)            /*    print a nibble as a hex character   */
  95. char nib; 
  96. {
  97.  
  98.     nib&=15;
  99.     putchar((nib >= 10) ? nib-10+'A': nib+'0');
  100. }
  101.  
  102. /** abort.c - print error messages & number then call _exit(FAIL)
  103.           9.15.83
  104. **/
  105.  
  106. abort( msg1 ,msg2 ,errno)       /*  print error msg1, msg2, and nbr */
  107. char *msg1,*msg2;           /*    Does not close files.  */
  108. short errno;
  109. {
  110.     fprintf(stderr,"ERR: ");
  111.     if (msg1)
  112.         fprintf(stderr,msg1);
  113.     if (msg2)
  114.         fprintf(stderr," ");
  115.     fprintf(stderr,msg2);
  116.     if (errno)    {
  117.         fprintf(stderr," %-d",errno);
  118.     }
  119.     exit(1);
  120. }
  121.